package com.bree.erp.service.stateless.facade;

import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import com.bree.erp.entity.BalanceId;
import com.bree.erp.entity.ReturnsBalance;
import com.bree.erp.service.ReturnsBalanceService;
import com.bree.erp.service.ReturnsService;
import com.bree.erp.service.ReturnsServiceFacade;

@Stateless
public class ReturnsServiceFacadeBean implements ReturnsServiceFacade {

	@EJB
	private ReturnsService returnsService;
	
	@EJB
	private ReturnsBalanceService balanceService;
	
	@Override
	public void addReturns(Date entryDate, Long projectId, String warehouseId, Long partUnitId,
			String location, double quantity, String remarks, String userId) {
		
		// create new return entry
		returnsService.create(entryDate, projectId, warehouseId, partUnitId, quantity, location, userId);
		
		// update balance
		BalanceId balanceId = new BalanceId(warehouseId, partUnitId);
		ReturnsBalance balance = balanceService.read(balanceId);
		if(balance == null) {
			balance = balanceService.create(warehouseId, partUnitId);
		}
		balanceService.update(balanceId, quantity);
	}

}
